package ar.edu.unicen.exa.isistan.archassis.view.dialogs;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import ar.edu.unicen.exa.isistan.archassis.ia.api.AIModuleFacade;
import ar.edu.unicen.exa.isistan.archassis.ia.api.db.ItemCase;
import ar.edu.unicen.exa.isistan.archassis.ia.api.db.JavaClass;
import ar.edu.unicen.exa.isistan.archassis.ia.api.db.ListCases;
import ar.edu.unicen.exa.isistan.archassis.view.Application;
import ar.edu.unicen.exa.isistan.archassis.view.classes.ClassesComposite;

public class AddCaseDialog extends Dialog {

	protected Object result;
	protected Shell shell;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public AddCaseDialog(Shell parent) {
		super(parent, 0);
		setText(Application.APPLICATION_NAME + " | Agregar caso");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.MAX | SWT.RESIZE | SWT.APPLICATION_MODAL);
		shell.setSize(450, 500);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		
		Button cancelButton = new Button(shell, SWT.NONE);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				exitAction(false);
			}
		});
		{//ubicacion del boton "Cancelar"
			FormData formData = new FormData();
			formData.bottom = new FormAttachment(100, -5);
			formData.right = new FormAttachment(100, -5);
			cancelButton.setLayoutData(formData);
		}
		cancelButton.setText("Cancelar");
		
		Button addButton = new Button(shell, SWT.NONE);
		{//ubicacion del boton "Agregar"
			FormData formData = new FormData();
			formData.bottom = new FormAttachment(100, -5);
			formData.right = new FormAttachment(cancelButton, -5);
			addButton.setLayoutData(formData);
		}
		addButton.setText("Agregar");
		
		addButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					saveCase();
					exitAction(true);
				} catch (EmptyDataFieldException e1) {
					Application.showErrorMessage(shell, "El campo [" + e1.getFieldLabel() + "] es requerido");
					//e1.printStackTrace();
				}
				
			}
		});
		
		helpLabel = new Label(shell, SWT.BORDER);
		{//ubicacion del label
			FormData formData = new FormData();
			formData.left = new FormAttachment(0, 5);
			formData.right = new FormAttachment(100, -5);
			formData.top = new FormAttachment(0, 5);
			helpLabel.setLayoutData(formData);
		}
		
		if (this.listCases != null) {
			helpLabel.setText("Agregar ejemplo al caso");
		} else {
			helpLabel.setText("Agregar caso nuevo");
		}
		
		Group listCasesGroup = new Group(shell, SWT.SHADOW_ETCHED_IN);
		listCasesGroup.setText("Datos del Caso");
		listCasesGroup.setLayout(new FormLayout());
		{//ubicacion del grupo "Datos del Caso"
			FormData formData = new FormData();
			formData.left = new FormAttachment(0, 5);
			formData.right = new FormAttachment(100, -5);
			formData.top = new FormAttachment(helpLabel, 5);
			//formData.height = 38;
			listCasesGroup.setLayoutData(formData);
		}
		
		listCasesNameText = new LabeledTextComposite(listCasesGroup, " Nombre       ", false);
		{
			FormData formData = new FormData();
			formData.left = new FormAttachment(0, 5);
			formData.right = new FormAttachment(100, -5);
			formData.top = new FormAttachment(0, 5);
			listCasesNameText.setLayoutData(formData);
		}
		
		listCasesDescriptionText = new LabeledTextComposite(listCasesGroup, " Descripcion", true);
		{
			FormData formData = new FormData();
			formData.left = new FormAttachment(0, 5);
			formData.right = new FormAttachment(100, -5);
			formData.top = new FormAttachment(listCasesNameText, 5);
			formData.bottom = new FormAttachment(100, -5);
			formData.height = 48;
			listCasesDescriptionText.setLayoutData(formData);
		}
		
		Group itemCaseGroup = new Group(shell, SWT.NONE);
		itemCaseGroup.setText("Datos del ejemplo");
		itemCaseGroup.setLayout(new FormLayout());
		{//ubicacion del grupo "Datos del ejemplo"
			FormData formData = new FormData();
			formData.left = new FormAttachment(0, 5);
			formData.right = new FormAttachment(100, -5);
			formData.top = new FormAttachment(listCasesGroup, 5);

			formData.height = 38;
			itemCaseGroup.setLayoutData(formData);
		}

		itemCaseDetailText = new LabeledTextComposite(itemCaseGroup, " Detalles        ", true);
		{
			FormData formData = new FormData();
			formData.right = new FormAttachment(100, -5);
			formData.top = new FormAttachment(0, 5);
			formData.left = new FormAttachment(0, 5);
			formData.bottom = new FormAttachment(100, -5);
			formData.height = 48;
			itemCaseDetailText.setLayoutData(formData);
		}
		
		classesComposite = new ClassesComposite(shell);
		{//ubicacion del composite con datos de las clases
			FormData formData = new FormData();
			formData.left = new FormAttachment(0, 5);
			formData.right = new FormAttachment(100, -5);
			formData.top = new FormAttachment(itemCaseGroup, 5);
			formData.bottom = new FormAttachment(addButton, -5);
			classesComposite.setLayoutData(formData);
		}
		
		if (this.listCases != null) {
			listCasesNameText.setText(listCases.getName());
			listCasesNameText.setEnabled(false);
			listCasesDescriptionText.setText(listCases.getDescripcion());
			listCasesDescriptionText.setEnabled(false);
		} else {
			//helpLabel.setText("Agregar caso nuevo");
		}
		
		classesComposite.change(this.classesList);
	}
	
	protected void saveCase() throws EmptyDataFieldException {
		
		/*
		 * Obtengo el valor del campo "Nombre"
		 */
		String listCaseName = listCasesNameText.getText();
		if (listCaseName.trim().equals("")) {
			throw new EmptyDataFieldException("Nombre");
		}
		
		/*
		 * Obtengo el valor del campo "Descripcion"
		 */
		String listCaseDescription = listCasesDescriptionText.getText();
		if (listCaseDescription.trim().equals("")) {
			throw new EmptyDataFieldException("Descripcion");
		}
		
		/*
		 * Obtengo el valor del campo "Detalles"
		 */
		String itemCaseDetail = itemCaseDetailText.getText();
		if (itemCaseDetail.trim().equals("")) {
			throw new EmptyDataFieldException("Detalles");
		}
		
		
		
		List<JavaClass> casesList = classesComposite.getClassesList();
		
		ItemCase itemCase = new ItemCase(casesList, itemCaseDetail);
		
		List<ItemCase> itemCaseList = new ArrayList<ItemCase>();
		itemCaseList.add(itemCase);
		
		if (listCases == null) {
			ListCases listCases2 = new ListCases(listCaseName,
					itemCaseList,
					listCaseDescription);
			AIModuleFacade.getInstance().addListCases(listCases2);
		} else {
			try {
				AIModuleFacade.getInstance().addItemCase(listCases.getId(), itemCase);
			} catch (Exception e) {
				Application.showErrorMessage(shell, e.getMessage());
				e.printStackTrace();
			}
		}
		
		
	}

	protected void exitAction(boolean b) {
		result = null;
		if (b) {
			result = new Object();
		}
		shell.dispose();
	}
	
	private List<JavaClass> classesList = null;
	private ListCases listCases = null;
	private Label helpLabel;
	private ClassesComposite classesComposite;
	private LabeledTextComposite itemCaseDetailText;
	private LabeledTextComposite listCasesNameText;
	private LabeledTextComposite listCasesDescriptionText;

	public void setClassesList(List<JavaClass> classesList) {
		this.classesList = classesList;
		
	}

	public void setListCases(ListCases listCases) {
		this.listCases = listCases;
		
	}
	
	protected Label getHelpLabel() {
		return helpLabel;
	}

}
